package com.yunc.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunc.base.utils.R;
import com.yunc.modules.sys.entity.SysDept;
import com.yunc.modules.sys.mapper.SysDeptMapper;
import com.yunc.modules.sys.service.ISysDeptService;
import com.yunc.modules.sys.service.ISysRoleDeptService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 部门
 * @author congcong tao
 * @create 2018/11/20 下午9:53
 */
@Service("sysDeptService")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper,SysDept> implements ISysDeptService {

    private final SysDeptMapper sysDeptMapper;
    private final ISysRoleDeptService sysRoleDeptService;

    @Override
    public List<SysDept> queryDeptList(String name) {
        List<SysDept> deptList = sysDeptMapper.queryDeptList();
        if (deptList != null && deptList.size() == 1) {
            deptList.get(0).setOpen(Boolean.TRUE);
        }
        return getTreeDept(deptList, "0", name);
    }

    @Override
    public String selectMaxCode(String parentId) {
        return sysDeptMapper.selectMaxCode(parentId);
    }

    @Override
    public R saveDept(SysDept dept) {
        dept.setCreateTime(new Date());
        // 查询code
        String parentId = dept.getParentId();
        String code = this.selectMaxCode(dept.getParentId());
        if (StringUtils.isNotEmpty(code)) {
            int i = Integer.parseInt(code) + 1;
            dept.setCode(String.valueOf(i));
        } else {
            // 取出父节点
            if (parentId != null) {
                SysDept parentDept = this.getById(parentId);
                dept.setCode(parentDept.getCode() + "01");
            } else{
                dept.setCode("10");
            }
        }
        boolean flag = this.save(dept);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    @Override
    public R updateDept(SysDept dept) {
        SysDept currentDept = this.getById(dept.getId());
        String parentId = dept.getParentId();
        if (!parentId.equals(currentDept.getParentId())) {
            String code = this.selectMaxCode(parentId);
            if (StringUtils.isNotEmpty(code)) {
                int i = Integer.parseInt(code) + 1;
                dept.setCode(String.valueOf(i));
            } else {
                // 取出父节点
                if (parentId != null) {
                    SysDept parentDept = this.getById(parentId);
                    dept.setCode(parentDept.getCode() + "01");
                }
            }
        }
        boolean flag = this.updateById(dept);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    @Override
    public List<String> listDeptByCode(List<String> deptCodeList) {
        return sysDeptMapper.listDeptByCode(deptCodeList);
    }

    @Override
    public List<SysDept> queryUserDeptList(String userId) {
        // 本部门及以下权限，有code可以直接like出子节点
        List<String> deptCodeList = sysRoleDeptService.listDeptChild(userId);
        return sysDeptMapper.listSysDeptByCode(deptCodeList);
    }

    private List<SysDept> getTreeDept(List<SysDept> deptList) {
        List<SysDept> subDeptList = new ArrayList<>();
        for (SysDept sysDept : deptList) {
            QueryWrapper<SysDept> ew = new QueryWrapper<SysDept>()
                    .eq("parent_id", sysDept.getId())
                    .orderByAsc("order_num");
            List<SysDept> deptListC = super.list(ew);
            if (deptListC != null && deptListC.size() > 0) {
                sysDept.setChildren(getTreeDept(deptListC));
            } else {
                sysDept.setIsLast(Boolean.TRUE);
            }
            subDeptList.add(sysDept);
        }
        return subDeptList;
    }

    private List<SysDept> getTreeDept(List<SysDept> deptList, String parentId, String name) {
        List<SysDept> result = new ArrayList<>();
        List<SysDept> temp;
        for (SysDept entity : deptList) {
            if (entity.getParentId().equals(parentId)) {
                temp = getTreeDept(deptList, entity.getId(), name);
                if (temp.size() > 0) {
                    entity.setChildren(temp);
                    result.add(entity);
                } else if (StringUtils.isEmpty(name) || StringUtils.contains(entity.getName(), name)) {
                    result.add(entity);
                }
            }
        }
        return result;
    }
}
